package cn.hjh.tools;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期格式转换辅助类
 */
public class StringToDateConverter implements Converter<String,Date> {

    private String datePattern;

    public StringToDateConverter(String datePattern){
        System.out.println("StringToDateConverter convert:"+datePattern);

        this.datePattern=datePattern;
    }

    @Override
    public Date convert(String s) {
        Date date=null;

        try {
            date=new SimpleDateFormat(datePattern).parse(s);

            System.out.println("StringToDateConverter convert date:"+date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }
}
